<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影视名称" prop="movieTitle">
        <el-input
          v-model="queryParams.movieTitle"
          placeholder="请输入影视名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观看时间">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:history:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['member:history:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" prop="historyId" width="80" />
      <el-table-column label="会员信息" align="center" width="150">
        <template #default="scope">
          <div style="display: flex; align-items: center; justify-content: center;">
            <el-avatar :size="30" :src="scope.row.memberAvatar || ''" style="margin-right: 8px;">
              <el-icon><UserFilled /></el-icon>
            </el-avatar>
            <span>{{ scope.row.memberNickname || `会员${scope.row.memberId}` }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="影视海报" align="center" prop="moviePoster" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.moviePoster"
            :preview-src-list="[scope.row.moviePoster]"
            fit="cover"
            v-if="scope.row.moviePoster"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><VideoPlay /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="影视名称" align="center" prop="movieTitle" :show-overflow-tooltip="true" />
      <el-table-column label="观看进度" align="center" prop="progress" width="200">
        <template #default="scope">
          <el-progress
            :percentage="scope.row.progress || 0"
            :status="scope.row.progress === 100 ? 'success' : ''"
          />
        </template>
      </el-table-column>
      <el-table-column label="观看时长" align="center" prop="watchDuration" width="100">
        <template #default="scope">
          {{ formatDuration(scope.row.watchDuration) }}
        </template>
      </el-table-column>
      <el-table-column label="最后观看时间" align="center" prop="lastWatchTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastWatchTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="观看次数" align="center" prop="watchCount" width="80">
        <template #default="scope">
          <el-tag>{{ scope.row.watchCount || 1 }}次</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['member:history:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="History">
import { listHistory, delHistory } from "@/api/member/history";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { UserFilled, VideoPlay } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const historyList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberId: null,
    movieTitle: null
  }
});

const { queryParams } = toRefs(data);

/** 格式化时长 */
function formatDuration(seconds) {
  if (!seconds) return '0分钟';
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  if (hours > 0) {
    return `${hours}小时${minutes}分钟`;
  }
  return `${minutes}分钟`;
}

/** 查询观看历史列表 */
function getList() {
  loading.value = true;
  const params = proxy.addDateRange(queryParams.value, dateRange.value);
  listHistory(params).then(response => {
    historyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.historyId);
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const historyIds = row.historyId || ids.value;
  proxy.$modal.confirm('是否确认删除观看历史编号为"' + historyIds + '"的数据项？').then(function() {
    return delHistory(historyIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/history/export', {
    ...queryParams.value
  }, `member_history_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}
</style>